import React from 'react';
import { Form, Input, Button, Switch, TreeSelect, Space, message } from 'antd';
import { SafetyCertificateOutlined } from '@ant-design/icons';
import { menuStore } from '../store/menuStore';
import { Menu } from '../store/menuTypes';

const { TextArea } = Input;

interface MenuFormProps {
  type: 'create' | 'edit' | 'view';
  initialValues?: any;
  onCancel: () => void;
}

//菜单树数据
const menuTreeData = [
  {
    title: '基本信息',
    value: 'dashboard',
  },
  {
    title: '通用设置',
    value: 'common',
    children: [
      {
        title: '用户设置',
        value: 'common-user',
      },
      {
        title: '角色设置',
        value: 'common-role',
      },
      {
        title: '菜单设置',
        value: 'common-menu',
      }
    ]
  },
  {
    title: '模型设置',
    value: 'model',
  },
  {
    title: '提示词设置',
    value: 'prompt',
  },
  {
    title: '函数设置',
    value: 'function',
  },
  {
    title: '智能体设置',
    value: 'agent',
  },
  {
    title: '数据监控',
    value: 'visualize',
  },
  {
    title: '智能场景设置',
    value: 'scene',
  },
  {
    title: '文件目录管理',
    value: 'directory',
  }
];


// 模拟操作权限数据
const operationOptions = [
  // { label: '创建', value: 'create' },
  { label: '查看', value: 'read' },
  // { label: '编辑', value: 'update' },
  // { label: '删除', value: 'delete' },
];

const getSelectedKeys = (treeData: any[]): string[] => {
  const keys: string[] = [];
  const traverse = (nodes: any[]) => {
    nodes.forEach(node => {
      keys.push(node.value);
      if (node.children) {
        traverse(node.children);
      }
    });
  };
  traverse(treeData);
  return keys;
};

const MenuForm: React.FC<MenuFormProps> = ({ type, initialValues, onCancel }) => {
  const [form] = Form.useForm();

  const onFinish = (values: Menu) => {
    values.parentId = '';
    menuStore.saveMenu(values);
    message.success(type === 'create' ? '创建成功' : '更新成功');
    onCancel();
  };

  const isViewMode = type === 'view';

  return (
    <Form
      form={form}
      layout="vertical"
      onFinish={onFinish}
      initialValues={{
        ...initialValues,
        status: initialValues?.status === 'active',
      }}
      disabled={isViewMode}
    >
      <Form.Item
        label="菜单名称"
        name="menuName"
        rules={[{ required: true, message: '请输入菜单名称！' }]}
      >
        <Input placeholder="请输入菜单名称" />
      </Form.Item>

      <Form.Item
        label="菜单编码"
        name="menuCode"
        rules={[{ required: true, message: '请输入菜单编码！' }]}
      >
        <Input placeholder="请输入菜单编码" />
      </Form.Item>

      <Form.Item
        label="菜单路径"
        name="menuUrl"
      >
        <Input placeholder="请输入菜单路径" />
      </Form.Item>

      <Form.Item
        label="上级菜单"
        name="parentId"
        rules={[{ required: false, message: '请选择上级菜单！' }]}
      >
        <TreeSelect
          treeData={menuTreeData}
          placeholder="请选择上级菜单"
          treeCheckable
          showCheckedStrategy="SHOW_PARENT"
          style={{ width: '100%' }}
        />
      </Form.Item>

      <Form.Item
        label="菜单类型"
        name="menuType"
      >
        <Input placeholder="请选择菜单类型" />
      </Form.Item>

      <Form.Item
        label="菜单图标"
        name="menuIcon"
      >
        <Input placeholder="请选择菜单图标" />
      </Form.Item>

      <Form.Item
        label="显示顺序"
        name="menuOrder"
      >
        <Input placeholder="请输入显示顺序" />
      </Form.Item>

      <Form.Item
        label="操作权限"
        name={['permissions', 'operations']}
        rules={[{ required: true, message: '请选择操作权限！' }]}
      >
        <TreeSelect
          treeData={operationOptions.map(item => ({
            title: item.label,
            value: item.value,
          }))}
          placeholder="请选择操作权限"
          treeCheckable
          showCheckedStrategy="SHOW_PARENT"
          style={{ width: '100%' }}
          defaultValue={getSelectedKeys(operationOptions)}
        />
      </Form.Item>

      <Form.Item
        label="状态"
        name="status"
        valuePropName="checked"
      >
        <Switch checkedChildren="启用" unCheckedChildren="禁用" />
      </Form.Item>

      {!isViewMode && (
        <Form.Item>
          <Space>
            <Button type="primary" htmlType="submit">
              {type === 'create' ? '创建' : '保存'}
            </Button>
            <Button onClick={onCancel}>取消</Button>
          </Space>
        </Form.Item>
      )}
    </Form>
  );
};

export default MenuForm; 